Sys : System status and setting

更新时间:
2024-05-13
下载文档

Sys : System status and setting

The system library provides a series of operating system related information and operation interfaces for applications, such as CPU, disk, memory and other information.

Support

The following shows sys module APIs available for each permissions.

 User ModePrivilege Mode
sys.appid
sys.eapid
sys.sleep
sys.error
sys.errno
sys.setErrno
sys.clearErrno
sys.version
sys.verCompare
sys.exec 
sys.gc
sys.hz
sys.srand
sys.assert
sys.cpuArch
sys.cpuInfo
sys.cpuBits
sys.cpuEndian
sys.cpuTotal
sys.cpuOnline
sys.memTotal
sys.memFree
sys.memKernFree
sys.diskTotal
sys.diskFree
sys.diskInfo 
sys.kernName
sys.kernVersion
sys.hostName
sys.engineName
sys.pageSize
sys.cacheLine
sys.perm
sys.monoTime
sys.setTime 
sys.setTz 
sys.getTz
sys.setPower 
sys.getPower
sys.reboot 
sys.tmpdir 
sys.secRegion 
sys.maxFds
sys.maxTasks

System

sys.appid()

  • Returns: {Integer} Application installation ID number.

Get the application installation ID number. This function is valid only in the EdgerOS environment.

sys.eapid()

  • Returns: {String} Application package ID number (bundle ID).

Get the application package ID number. This function is valid only in the EdgerOS environment.

sys.sleep(ms[, wakeupByInt])

  • ms {Integer} Waiting for the specified time in milliseconds.
  • wakeupByInt {Boolean} Whether to allow wakeup by interrupt. default: false.
  • Returns: {Integer} If wake up earlier, return the remaining time, otherwise 0.

The current task sleeps for a specified number of milliseconds. If wakeupByInt is true, if a Timer or SigSlot interrupt occurs, it will exit immediately and return the remaining time.

Example

// Print 'Hello World!' per second.
while (true) {
  console.log('Hello World!');
  sys.sleep(1000);
}

sys.error([errno])

  • errno {Integer} Error number. default: sys.errno().
  • Returns: {String} Error number information.

Get the error message string according to the error number. If there are no parameters, the last system error number is used.

Example

// Print last error information.
console.log(sys.error());
// Same as:
console.log(sys.error(sys.errno()));

sys.errno()

  • Returns: {Integer} Last error number.

Get system last error number, such as: failed to open device or file, device or file read and write failure, failed to query disk information, domain name resolution failed, process creation failed, network transfer failed, etc. and can be used this method to get error information.

sys.setErrno(errno)

  • errno {Integer} Error number.

Set current errno to the specified value.

sys.clearErrno()

Clear current errno (set errno to zero).

sys.version()

  • Returns: {Array} JSRE environment version number.

The return value is an array containing three items, [x, y, z] xyz are integers, x is the major version number, y is minor version number, and z is patch version number.

Example

console.log(sys.version());

sys.verCompare(ver1, ver2)

  • ver1 {Array} Version 1 array.
  • ver2 {Array} Version 2 array.
  • Returns: {Integer} 1: ver1 > ver2, -1: ver1 < ver2, 0: ver1 == ver2.

Compare version numbers.

Example

var ret = sys.verCompare(sys.version(), [0, 5, 6]);
if (ret > 0) {
  console.log('The current version number is newer than 0.5.6');
}

sys.exec(command)

  • command {String} Shell command.
  • Returns: {Integer} Shell command return value.

Run a shell command.

Example

// Show process list.
var ret = sys.exec('ps');

sys.gc([slow])

  • slow {Boolean} Whether free as much memory as possible. default: false.

System garbage collection. If slow is true, free as much memory as possible, else free unused objects, but keep memory allocated for performance improvements.

sys.hz()

  • Returns: {Integer} operating system ticks frequency.

Get operating system ticks frequency.

sys.srand(seed)

  • seed {Integer} Random number generator seed.
  • Returns: {Integer} Random number generator seed currently in use.

Set the random number generator seed.

Example

var mono = sys.monoTime();
sys.srand(mono / 1000);

sys.assert(cond)

  • cond {Boolean} Assertion condition.
  • Returns: {Boolean} The condition is true, return true, otherwise current process will exit.

This method determines whether the cond condition is true. If it is true, continue execute. If condition is false, the current process will exit.

sys.cpuArch()

  • Returns: {String} Current CPU architecture.

Get current CPU architecture.

sys.cpuInfo()

  • Returns: {String} Current CPU model information.

Get current CPU model information.

sys.cpuBits()

  • Returns: {Integer} Current CPU word length.

Get current CPU word length 32 or 64.

sys.cpuEndian()

  • Returns: {Integer} Current CPU endian.

Get current CPU endian, 1: big endian 0: little endian.

sys.cpuTotal()

  • Returns: {Integer} Current number of CPU cores.

Get current number of CPU cores.

sys.cpuOnline()

  • Returns: {Integer} Number of cores currently running.

Get number of cores currently running.

sys.memTotal()

  • Returns: {Integer} Total size of physical memory.

Get total size of physical memory.

sys.memFree()

  • Returns: {Integer} Free size of physical memory.

Get free size of physical memory.

sys.memKernFree()

  • Returns: {Integer} Free size of kernel memory.

Get free size of kernel memory.

sys.diskTotal([aux])

  • aux {Boolean} Whether to get service storage information.
  • Returns: {Integer} Total size of disk in MBytes.

Get Total size of disk in MBytes. Negative indicate errors.

sys.diskFree([aux])

  • aux {Boolean} Whether to get service storage information.
  • Returns: {Integer} Free size of disk in MBytes.

Get free size of disk in MBytes. Negative indicate errors.

sys.diskInfo(blkdev)

  • blkdev {String} Block device name.
  • Returns: {Object} Block device information object.

Get specified block device information. The obtained information object has the following members:

  • size {Number} Block device size in MB. EdgerOS 1.9.8 add this member.
  • type {String} Block device type.
  • serial {String} Block device serial number.
  • firmware {String} Block device firmware information.
  • product {String} Block device product information.
  • media {String} Storage media information.

Device type may include the following types:

TypeDescription
'RAM'Memory block device
'RAW'RAW block device.
'ATA'ATA block device.
'SATA'SATA block device.
'SCSI'Common SCSI block device.
'SAS'SAS SCSI block device.
'UFS'UFS SCSI block device.
'NVME'NVMe block device.
'SDMMC'SD/MMC block device.
'MSTICK'Memory stick block device.
'USB'USB mass storage block device.
'OTHER'Other type block device.

Example

var info = sys.diskInfo('hdd0');

sys.kernName()

  • Returns: {String} Operating system kernel name.

Get operating system kernel name.

sys.kernVersion()

  • Returns: {Array} Operating system kernel version number.

The return value is an array containing three items, [x, y, z] xyz are integers, x is the major version number, y is minor version number, and z is patch version number.

Example

console.log(sys.kernVersion());

sys.hostName()

  • Returns: {String} Current machine host name.

Get current machine host name.

sys.engineName()

  • Returns: {String} Current engine name.

Get current engine name.

sys.pageSize()

  • Returns: {Integer} Memory page size.

Get memory page size. Typical page sizes include 4KB, 16KB, 64KB...

sys.cacheLine()

  • Returns: {Integer} Data cache line size.

Get data cache line size. Typical cache line size 16, 32, 64...

sys.perm()

  • Returns: {Integer} Current app permission level.

Get current app permission level.

  • sys.PERM_USER User mode.
  • sys.PERM_PRIVILEGE Privilege mode.

Example

var perm = sys.perm();
if (perm === sys.PERM_PRIVILEGE) {
  // We are in privilege mode.
}

sys.monoTime()

  • Returns: {Integer} Current machine running time from power on in milliseconds.

Get machine running time from power on.

Example

console.log(sys.monoTime()); // Operating system running time in MS.

sys.setTime([newTime])

  • newTime {Integer} New system time the number of milliseconds from 1970.01.01 to the present. default: current time.
  • Returns: {Boolean} Returns true if the setting succeeds, otherwise false.

Set new system time and synchronized to the physical clock chip, newTime must be UTC time.

Example

var d = new Date();
var n = d.getTime(); // Get 1970.01.01 to now time of milliseconds
n += 3600 * 1000; // Time forward one hour.
sys.setTime(n);

sys.setTz(tzStr)

  • tzStr {String} Timezone string.

Set system timezone.

Example

// Beijing time: GMT+8:00:00
sys.setTz('GMT+8:00:00');

sys.getTz()

  • Returns: {Integer} UTC time minus local time deviation (seconds).

Get current system time zone information: Returns value = UTC time - Local Time. For example, 'GMT+8:00:00' is -28800.

sys.setPower(level)

  • level {String} Power level.

Set CPU performance, not all CPUs support performance settings. Acceptable CPU performance includes the following levels:

  • 'top' maximum performance.
  • 'fast' higher performance.
  • 'normal' normal performance.
  • 'slow' low performance.

sys.getPower()

  • Returns: {String} Power level.

Get the current CPU performance level.

sys.reboot([stop[, method]])

  • stop {Boolean} Whether to stop all JSRE processes. default: false.
  • method {String} Reboot method. default: undefined.

method can be: 'reboot'(reboot this machine) or 'shutdown'(shutdown this machine) or undefined(do not shutdown or reboot this machine).

Example

sys.reboot(true); // Stop all JSRE process.
sys.reboot(true, 'shutdown'); // Stop all JSRE process and shutdown this machine.
sys.reboot(true, 'reboot'); // Stop all JSRE process and reboot this machine.

sys.tmpdir()

  • Returns: {String} The temporary folder.

Get the operating system temporary folder, typical path is '/tmp/'.

sys.secRegion([newRegion])

  • newRegion {Integer} Current process new security region.
  • Returns {Integer} Current process current security region.

Set or get current process security region.

sys.maxFds()

  • Returns: {Integer} The maximum number of files that the current process can open.

Get the maximum number of files that the current process can open.

sys.maxTasks()

  • Returns: {Integer} The maximum number of task can create in current process.

Get the maximum number of task can create in current process.

文档内容是否对您有所帮助?
有帮助
没帮助